﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using Cinch;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Loc;
namespace SLS.Document
{
    public partial class DocumentManagement
    {
        string root = DataAccessService.Instance.Appliationpath;
        public void RegisterDocument(string doctype)
        {
            string nameUI = doctype;
            string roottype= doctype;
            string nameWF = doctype;
            int length = 0;
            try
            {
                switch (doctype)
                {
                    case "DocumentCenter":
                        nameUI = "General";
                        nameWF = "CenterWorkFlow";
                        break;
                    default:
                        length = doctype.Length;
                        if (doctype.StartsWith("VIP"))
                        {
                            roottype = doctype.Substring(3, length - 3);
                         }
                        if (doctype.StartsWith("Edit"))
                        {
                            roottype = doctype.Substring(4, length - 4);  
                        }
                        if (doctype.StartsWith("DATA"))
                        {
                            roottype = doctype.Substring(4, length - 4);
                        }
                        if (doctype.StartsWith("Exp"))
                        {
                            roottype = doctype.Substring(3, length - 3);
                        }
                        break;
                }
                if (roottype != doctype)
                {
                    nameUI = roottype + "VW";
                    nameWF = roottype + "WF";
                }
                LoadAssembly(nameWF);
                LoadAssembly(nameUI);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void AddToDocumentList(ObservableCollection<WorkspaceData> list, WorkspaceData workspacedata)
        {
            try
            {
                if (!list.Contains(workspacedata))
                {
                    list.Add(workspacedata);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void LoadAssembly(ArrayList namelist)
        {
            foreach (var name in namelist)
            {
                LoadAssembly((string)name);
            }
        }
        private Assembly TryLoad(string name)
        {
            Assembly assembly = null;
            try
            {
                string path = root+@"\"+name;
                if (File.Exists(path))
                {
                    assembly = Assembly.LoadFrom(path);
                    return assembly;
                }
                path = root + @"\SharedDll\"+name;
                if (File.Exists(path))
                {
                    assembly = Assembly.LoadFrom(path);
                    return assembly;
                }
                
                path = root + @"\CLSLib\" + name;
                if (File.Exists(path))
                {
                    assembly = Assembly.LoadFrom(path);
                    return assembly;
                }
                path = root + @"\UILib\" + name;
                if (File.Exists(path))
                {
                    assembly = Assembly.LoadFrom(path);
                    return assembly;
                }

                path = root + @"\WFLib\" + name;
                if (File.Exists(path))
                {
                    assembly = Assembly.LoadFrom(path);
                    return assembly;
                }
                 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return assembly;
        }
        public void LoadAssembly(string name)
        {
            if (name.Contains("CenterWorkFlow"))
            {
                Tracer.Info(name, name);
            }
            Assembly assembly = null;
            try
            {
                if (AssemblyManager.ReflectionCache.ProcessedAssemblies.ContainsKey(name))
                    return;
                if (AssemblyManager.ReflectionCache.CurrentDomainAssemblies.ContainsKey(name))
                {
                    assembly = AssemblyManager.ReflectionCache.CurrentDomainAssemblies[name].Assembly;
                }
                else
                { 
                    assembly = TryLoad(name + ".dll");
                    AssemblyName assamblyName = AssemblyName.GetAssemblyName(assembly.Location);

                    if (assembly == null)
                        return;
                    AssemblyManager.ReflectionCache.AddToCurrentDomain(assembly);
                    AssemblyManager.ReflectionCache.LoadAssembly(assembly);
                    AppDomain.CurrentDomain.Load(assamblyName);                    
                }
                ViewResolver.ResolveViewLookups(assembly);
                PopupResolver.ResolvePopupLookups(assembly);
                LocalizationManager.RegisterAssembly(assembly);
             /*    
                AssemblyCatalog catalog = new AssemblyCatalog(assembly);
                CompositionContainer container = new CompositionContainer(catalog);
                 CompositionBatch bat = new CompositionBatch(); 
                bat.AddPart(this);
                container.Compose(bat); 
             AssemblyName[] reflist = assembly.GetReferencedAssemblies();
              foreach (AssemblyName ass in reflist)
               {
                   name = ass.Name;
                   LoadAssembly(name);
               }*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void LoadAssembly(Assembly assembly)
        {
            string name = "";
            try
            {
                name = assembly.GetName().Name;
                if (AssemblyManager.ReflectionCache.ProcessedAssemblies.ContainsKey(name))
                    return;
                ViewResolver.ResolveViewLookups(assembly);
                PopupResolver.ResolvePopupLookups(assembly);
                LocalizationManager.RegisterAssembly(assembly);
                //首先，对于已经加载到内存的dll，无需再次加载。 
                AssemblyManager.ReflectionCache.RefreshCurrentDomain();
                /* 
             foreach(KeyValuePair<string,AssemblyRef> kp in AssemblyManager.ReflectionCache.CurrentDomainAssemblies)
             {
                 AssemblyRef assemblyref = (AssemblyRef)kp.Value;
                 name = kp.Key;
                 LoadAssembly(name);
             }

         AssemblyName[] reflist = assembly.GetReferencedAssemblies();
             foreach (AssemblyName ass in reflist)
             {
                 name = ass.Name;
                 LoadAssembly(name);
             }*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}


